<?php
/*
 * 初始化状态
 * @arrange Mark
 * @package ixdcw
 * @file    init.inc.php
 * @time    12-12-19 下午1:04
 */
defined('IN_IXDCW') or exit('Access Denied');
isset($file) or $file = 'homepage';
if(isset($update) || isset($preview)) { // 更新或者是预览
	$db->cids = 1;
	$CACHE_HOME = '';
} else {
	$CACHE_HOME = $DT['cache_home'] ? DT_CACHE.'/php/'.substr($username, 0, 2).'/'.$username.'.php' : '';
	if($file == 'homepage' && $CACHE_HOME && is_file($CACHE_HOME) && $DT_TIME - @filemtime($CACHE_HOME) < $DT['cache_home']) {
		/*
		    here delete this 13 characters
		    <?php exit;?>
		*/
        exit(substr(file_get_contents($CACHE_HOME), 13));
	}
}
$sql = "SELECT * FROM {$table_member} m,{$table} c WHERE m.userid=c.userid AND m.username='$username' LIMIT 0,1";
$COM = $db->getOne($sql, 'CACHE');
$COMGROUP = PCache::read('group-'.$COM['groupid'].'.php');
/*if(!$COM || $COMGROUP['memtype'] < 5) {
	$dc->rm(md5($sql));
	$head_title = $L['not_company'];
	@header("HTTP/1.1 404 Not Found");
	include template('com-notfound', 'message');
	exit;
}

if(!$COM['edittime'] && !$MOD['openall']) {
	$head_title = $COM['company'];
	@header("HTTP/1.1 404 Not Found");
	include template('com-opening', 'message');
	exit;
}*/
// 存在公司域名
$domain = $COM['domain'];
if($domain) {
	if(!isset($preview) && !isset($update)) {
		if($CFG['com_domain']) {
			if(strpos($DT_URL, $domain) === false) {
				$subdomain = userurl($username);
				if(strpos($DT_URL, $subdomain) === false) {
					dheader('http://'.$domain.'/');
				} else {
					if($DT_URL == $subdomain.'index.php' || $DT_URL == $subdomain) dheader('http://'.$domain.'/');
					dheader(str_replace($subdomain, 'http://'.$domain.'/', $DT_URL));
				}
			}
		} else {
			if(strpos($DT_URL, $domain) === false) dheader(userurl($username, ($file && $file != 'homepage') ? 'file='.$file : '', $domain));
		}
	}
	$DT['rewrite'] = intval($CFG['com_rewrite']);
}
$userid = $COM['userid'];

// 更新URL
if($COM['linkurl'] != $linkurl) {
	$COM['linkurl'] = $linkurl;
	$db->query("UPDATE LOW_PRIORITY {$table} SET linkurl='$linkurl' WHERE userid=$userid", 'UNBUFFERED');
}

// VIP Expired
if($MOD['delvip'] && $COM['vip'] && $COM['totime'] && $COM['totime'] < $DT_TIME) {
	$COM['vip'] = 0;
	$COM['groupid'] = $gid = $COM['regid'] == 5 ? 5 : 6;
	$COM['skin'] = $COM['template'] = '';
	$db->query("UPDATE {$table} SET groupid=$gid,vip=0,styletime=0,skin='',template='' WHERE userid=$userid");
	$db->query("UPDATE {$db->getPre()}member SET groupid=$gid WHERE userid=$userid");
}

// SKIN Expired
if($COM['styletime'] && $COM['styletime'] < $DT_TIME) {
	$COM['skin'] = $COM['template'] = '';
	$db->query("UPDATE {$table} SET styletime=0,skin='',template='' WHERE userid=$userid");
}
$COM['year'] = vip_year($COM['fromtime']);


// 没有公司主页, 展示会员基本信息
if(!isset($COMGROUP['homepage']) || !$COMGROUP['homepage']) {
	$head_title = $COM['company'];
	$head_keywords = $COM['keyword'];
	$head_description = $COM['introduce'];
	$member = $COM;
	$content_table = content_table(4, $userid, is_file(DT_CACHE.'/4.part'), $db->getPre().'company_data');
	$r = $db->getOne("SELECT content FROM {$content_table} WHERE userid=$userid", 'CACHE');
	$content = $r['content'];
	$member['thumb'] = $member['thumb'] ? $member['thumb'] : CSSIMG_URL.'common/company.jpg';

	$id = CompanyEntry::getIdByHomepage($homepage);
	if($id) $companyInfo = CompanyEntry::getShowInfoById($id);
	$did = $id;
	$table   = Table::name(Table::NAME_MERCHANT_DOC);
	$topId   = CompanyEntry::getTopDown($table,$did,'down','did');
	$downId  = CompanyEntry::getTopDown($table,$did,'top','did');


	include template('show', $module);
	exit;
}

// 我勒个去,这里的MOD是指公司的MOD啊,我以为是member,坑爹啊

/** @var $api_map string    map的api接口  */
$api_map   = ($MOD['map'] && $COMGROUP['map']) ? $MOD['map'] : '';

/** @var $api_stats string  统计接口 */
$api_stats = ($MOD['stats'] && $COMGROUP['stats']) ? $MOD['stats'] : '';

/** @var $api_kf string     客服接口 */
$api_kf    = ($MOD['kf'] && $COMGROUP['kf']) ? $MOD['kf'] : '';
isset($rewrite) or $rewrite = '';
if($rewrite) {
	$r = explode('-', $rewrite);
	$rc = count($r);
	if($rc%2 == 0) {
		for($i = 0; $i < $rc; $i++) {
			if(in_array($r[$i], array('itemid', 'typeid', 'page', 'view', 'kw', 'preview', 'update'))) {
				$$r[$i] = $r[++$i];
			} else {
				++$i;
			}
		}
	}
	$page = isset($page) ? max(intval($page), 1) : 1;
	$catid = isset($catid) ? intval($catid) : 0;
	$itemid = isset($itemid) ? (is_array($itemid) ? $itemid : intval($itemid)) : 0;
	$kw = isset($kw) ? htmlspecialchars(str_replace(array("\'"), array(''), trim(urldecode($kw)))) : '';
	if(strlen($kw) < $DT['min_kw'] || strlen($kw) > $DT['max_kw']) $kw = '';
	$keyword = $kw ? str_replace(array(' ', '*'), array('%', '%'), $kw) : '';
}
include load('homepage.lang');
in_array($file, $MFILE) or dheader($MOD['linkurl']);
if($COMGROUP['menu_d']) {
	$_menu_show = array();
	foreach($HMENU as $k=>$v) {
		$_menu_show[$k] = strpos(','.$COMGROUP['menu_d'].',', ','.$k.',') !== false ? 1 : 0;
	}
	$_menu_show = implode(',', $_menu_show);
} else {
	$_menu_show = '1,1,1,1,1,1,1,1,0,0,0,0,0,0';
}
$_menu_order = '0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150, 160';
$_menu_num = '1,16,30,30,10,30,1,12,12,12,12,30,12,1,4,4';
$_menu_file = implode(',' , $MFILE);
$_menu_name = implode(',' , $HMENU);

if($COMGROUP['side_d']) {
	$_side_show = array();
	foreach($HSIDE as $k=>$v) {
		$_side_show[$k] = strpos(','.$COMGROUP['side_d'].',', ','.$k.',') !== false ? 1 : 0;
	}
	$_side_show = implode(',', $_side_show);
} else {
	$_side_show = '1,1,1,0,1,0,1';
}
$_side_order = '0,10,20,30,40,50,60';
$_side_num = '1,5,10,1,1,5,5';
$_side_file = implode(',' , $SFILE);
$_side_name = implode(',' , $HSIDE);

$HOME = get_company_setting($COM['userid'], '', 'CACHE');


//if(isset($HOME['menu_file'])) $HOME['menu_file'] = str_replace('credit', 'honor', $HOME['menu_file']);//For 3.x
//if(isset($HOME['side_file'])) $HOME['side_file'] = str_replace('credit', 'honor', $HOME['side_file']);//For 3.x

$menu_show = explode(',', isset($HOME['menu_show']) ? $HOME['menu_show'] : $_menu_show);
$menu_order = explode(',', isset($HOME['menu_order']) ? $HOME['menu_order'] : $_menu_order);
$menu_num = explode(',', isset($HOME['menu_num']) ? $HOME['menu_num'] : $_menu_num);
$menu_file = explode(',', isset($HOME['menu_file']) ? $HOME['menu_file'] : $_menu_file);
$menu_name = explode(',', isset($HOME['menu_name']) ? $HOME['menu_name'] : $_menu_name);
$_HMENU = array();
asort($menu_order);
foreach($menu_order as $k=>$v) {
	$_HMENU[$k] = $HMENU[$k];
}
$HMENU = $_HMENU;

$MENU = array();
$menuid = 0;
foreach($HMENU as $k=>$v) {
	if($menu_show[$k] && in_array($menu_file[$k], $MFILE)) {
		$MENU[$k]['file'] = $menu_file[$k];
		$MENU[$k]['name'] = $menu_name[$k];
		if($menu_file[$k] !== 'homepage'){$linkurl.='/f-'.$menu_file[$k].'/';}
		$MENU[$k]['linkurl'] = 	$linkurl;

	}
	if($file == $menu_file[$k]) $menuid = $k;
	if($menu_num[$k] < 1 || $menu_num[$k] > 50) $menu_num[$k] = 10;
}
$side_show = explode(',', isset($HOME['side_show']) ? $HOME['side_show'] : $_side_show);
$side_order = explode(',', isset($HOME['side_order']) ? $HOME['side_order'] : $_side_order);
$side_num = explode(',', isset($HOME['side_num']) ? $HOME['side_num'] : $_side_num);
$side_file = explode(',', isset($HOME['side_file']) ? $HOME['side_file'] : $_side_file);
$side_name = explode(',', isset($HOME['side_name']) ? $HOME['side_name'] : $_side_name);
$_HSIDE = array();
asort($side_order);
foreach($side_order as $k=>$v) {
	if($side_show[$k] && in_array($side_file[$k], $SFILE)) {
		$_HSIDE[$k] = $HSIDE[$k];
	}
	if($side_num[$k] < 1 || $side_num[$k] > 50) $side_num[$k] = 10;
}
$HSIDE = $_HSIDE;
$side_pos = isset($HOME['side_pos']) && $HOME['side_pos'] ? 1 : 0;
$side_width = isset($HOME['side_width']) && $HOME['side_width'] ? $HOME['side_width'] : 200;
$show_stats = isset($HOME['show_stats']) && $HOME['show_stats'] == 0 ? 0 : 1;
$skin = 'default';
$template = 'homepage';
if($COM['skin'] && $COM['template']) {
	$skin = $COM['skin'];
	$template = $COM['template'];
} else if($COMGROUP['styleid']) {
	$r = $db->getOne("SELECT skin,template FROM {$db->getPre()}style WHERE itemid=$COMGROUP[styleid]", 'CACHE');
	if($r) {
		$skin = $r['skin'];
		$template = $r['template'];
	}
}
$preview = isset($preview) ? intval($preview) : 0;
if($file == 'homepage') {
	if($preview) {
		$preview = $db->getOne("SELECT * FROM {$db->getPre()}style WHERE itemid={$preview}");
		if($preview) {
			$skin = $preview['skin'];
			$template = $preview['template'];
		}
	}
}

// 五幅banner
$banners = array();
if (isset($HOME['banner1']) && !empty($HOME['banner1'])) $banners[] = $HOME['banner1'];
if (isset($HOME['banner2']) && !empty($HOME['banner2'])) $banners[] = $HOME['banner2'];
if (isset($HOME['banner3']) && !empty($HOME['banner3'])) $banners[] = $HOME['banner3'];
if (isset($HOME['banner4']) && !empty($HOME['banner4'])) $banners[] = $HOME['banner4'];
if (isset($HOME['banner5']) && !empty($HOME['banner5'])) $banners[] = $HOME['banner5'];

// baner 的高度和宽度
$bannerw 		= (isset($HOME['bannerw']) && $HOME['bannerw']) ? intval($HOME['bannerw']) : 960;
$bannerh 		= (isset($HOME['bannerh']) && $HOME['bannerh']) ? intval($HOME['bannerh']) : 200;


// 是否允许评论
$could_comment 	= $MOD['comment'];
// 公司主页信息链接到主站, 这个真他妈没用了. 已经完全连接到黄页了.
$homeurl 		= $MOD['homeurl'];
if($domain) {
	$could_comment = false;
}
$group=PCache::read("grouporder.php");//读取缓存会员组排序
if(!$group){
    Xd_save_groupcachefile();
}
// 会员组查看联系人信息
$could_contact 	= check_group($_groupid, $MOD['group_contact']) ||  (in_array($COM["groupid"],explode(',',$group["type8"])) && $_userid);
if($username == $_username || $domain) {
	$could_contact = true;
}

// 背景图
$background       = isset($HOME['background']) ? $HOME['background'] : '';
// 背景色
$bgcolor          = isset($HOME['bgcolor']) ? $HOME['bgcolor'] : '';
// logo
$logo             = isset($HOME['logo']) ? $HOME['logo'] : '';
// 视频
$video            = isset($HOME['video']) ? $HOME['video'] : '';
// 公告
$announce         = isset($HOME['announce']) ? $HOME['announce'] : '';
// 地图
$map              = isset($HOME['map']) ? $HOME['map'] : '';
// 统计
$stats            = isset($HOME['stats']) ? $HOME['stats'] : '';
// 客服
$kf               = isset($HOME['kf']) ? $HOME['kf'] : '';
// 头部标题
$head_title       = $MENU[$menuid]['name'];
// seo关键字啥的
$seo_keywords     = isset($HOME['seo_keywords']) ? $HOME['seo_keywords'] : '';
$seo_description  = isset($HOME['seo_description']) ? $HOME['seo_description'] : '';
$head_keywords    = strip_tags($seo_keywords ? $seo_keywords : $COM['company'].','.str_replace('|', ',', $COM['business']));
$head_description = strip_tags($seo_description ? $seo_description : $COM['introduce']);
if($DT['cache_hits']) {
	 cache_hits($moduleid, $userid);
} else {
	$db->query("UPDATE LOW_PRIORITY {$table} SET hits=hits+1 WHERE userid=$userid", 'UNBUFFERED');
}
include DT_CORE.'/company/'.$file.'.inc.php';